package decoder

import "iot-protocol-oob/codec"

type ModbusRTUDecoder struct {
	Buf   []byte
	Cache []byte
}

type ModbusTCPDecoder struct {
	Buf   []byte
	Cache []byte
}

func (d ModbusRTUDecoder) Decode() ([]byte, []byte) {

	d.Buf = append(d.Cache, d.Buf...)
	length := uint16(len(d.Buf))
	if length < 3 {
		d.Cache = d.Buf
		return []byte{}, d.Cache
	}
	messageLength := uint16(d.Buf[codec.LengthIndex]) + uint16(5)
	if length < messageLength {
		d.Cache = d.Buf
		return []byte{}, d.Cache
	} else if length == messageLength {
		d.Cache = []byte{}
		return d.Buf, d.Cache
	} else {
		d.Cache = d.Buf[messageLength:]
		return d.Buf[0:messageLength], d.Cache
	}
}

func (d ModbusTCPDecoder) Decode() ([]byte, []byte) {

	return nil, nil
}
